//
//  MobileAppsTipAppViewController.m
//  MobileAppsTipApp
//
//  Created by jcotter on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MobileAppsTipAppViewController.h"


@implementation MobileAppsTipAppViewController

@synthesize label_TipAmount;
@synthesize label_BillTotal;
@synthesize label_Tax;
@synthesize textField_Subtotal;
@synthesize textField_tax;
@synthesize sw_includeInTip;
@synthesize TipSelector;

@synthesize m_customtipview;
@synthesize m_standardtipview;
@synthesize segControl;

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    m_guest = [Guest alloc];
    
/*    // now, create a button to increment our counter
	UIButton *coolButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	// set the text on our button for all states
	[coolButton setTitle:@"Increment" forState:UIControlStateNormal];
*/	
	// when someone releases a button, call our click handler
	[sw_includeInTip addTarget:self
				   action:@selector(clickHandler:)
		 forControlEvents:UIControlEventTouchUpInside];
    
    [segControl addTarget:self action:@selector(segControlHandler:) forControlEvents:UIControlEventValueChanged];

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == textField_Subtotal)
    {
        NSString *mySubtotal = textField.text;
        [m_guest setSubtotal:[mySubtotal doubleValue]];
    }
    else if(textField == textField_tax)
    {
        NSString *myTax = textField.text;
        [m_guest setTaxAmount:[myTax doubleValue]];
    }
    [self updateUI];

        
    NSLog(@"typed %@", textField.text); // %@ for any NSObject
    return YES; // allow the default return behavior as well 
}

- (void)updateUI
{
    double myBillTotal = [m_guest getTotal];
    label_BillTotal.text = [NSString stringWithFormat:@"BillTotal: $%.02f", myBillTotal];
    
    double myTip = [m_guest getTipTotal];
    label_TipAmount.text = [NSString stringWithFormat:@"TipTotal: $%.02f", myTip];
}

#pragma mark -
#pragma mark Handle Button Clicking
-(IBAction) clickHandler:(id)sender {
    UISwitch *button = sender;
    BOOL buttonState = button.on;
    [m_guest setUseTaxInTip:buttonState];
    [self updateUI];
}

/*#pragma mark -
#pragma mark Handle Button Clicking
-(IBAction) segControlHandler:(id)sender {
    UISegmentedControl *seg = sender;

    [self updateUI];
    
}
*/

/*- (IBAction)segmentSwitch:(id)sender{
    UISegmentedControl *segControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segControl.selectedSegmentIndex;
    
    if (selectedSegment == 0){
        [MobileAppsTipAppViewController setHidden:NO];
        [CustomTipUIViewController setHidden:YES];
    }
    else{
        //toggle the correct view to be visible
        [MobileAppsTipAppViewController setHidden:YES];
        [CustomTipUIViewController setHidden:NO];
    }
}
*/

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [textField_Subtotal resignFirstResponder];
    [textField_tax resignFirstResponder];
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    return 7;
}


- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    
    
    return [NSString stringWithFormat:@"%.02f%@", arrayNo[row], @"%"];

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    double tipperc = arrayNo[row] / 100.0;         
    [m_guest setTipPerc:tipperc];
    [self updateUI];
}
@end
